<template>
	<view>
		<view class="ffffff mainpadding_top flexbetween ">
			<view class="hei_text bold titletext">活動名稱</view>
			<input type="text" class="hei_text main_size bold" style="text-align: right;" placeholder="請輸入活動名稱"
				v-model="name">
		</view>
		<view class="ffffff mainpadding_top ">
			<view class="hei_text bold titletext">分享文字管理</view>
			<u--textarea v-model="share_title" class="margin_top" placeholder="" maxlength="-1"
				autoHeight></u--textarea>
		</view>
		<view class="flexbetween ffffff mainpadding_top ">
			<view class="main_size bold hei_text">圖片</view>
			<view class="flexleft">
				<view class="positionr margin_right1" v-for="(item,index) in image_text" :key="index">
					<image class="listleftimg radius" :src="item" mode=""></image>
					<view class="deleteimg" @click="removeImg(image,image_text,index)"></view>
				</view>
				<image class="listleftimg radius" src="@/static/image/system/upimg.png" mode=""
					v-if="image_text.length<1" @click="uploadImg(image,image_text,1)"></image>
			</view>
		</view>
		<view class="flexbetween ffffff mainpadding_top ">
			<view class="main_size bold hei_text">輪播圖</view>
			<view class="flexleft">
				<view class="positionr margin_right1" v-for="(item,index) in images_text" :key="index">
					<image class="listleftimg radius" :src="item" mode=""></image>
					<view class="deleteimg" @click="removeImg(images,images_text,index)"></view>
				</view>
				<image class="listleftimg radius" src="@/static/image/system/upimg.png" mode=""
					v-if="images_text.length<3" @click="uploadImg(images,images_text,3)"></image>
			</view>
		</view>
		<view class="flexbetween ffffff mainpadding_top">
			<view class="main_size bold hei_text">影片</view>
			<view v-if="video_url" @click="delvideo(1)">
				<u-icon name="trash" color="#ff0000" size="18"></u-icon>
			</view>
		</view>
		<view class="ffffff mainpadding2">
			<view class="upzhizhao flexcenter" v-if="!video_url" @click="upvideo(1)">
				<u-icon name="plus" color="#999" size="28"></u-icon>
			</view>
			<view v-if="video_url" class="upzhizhao " style="position: relative;">
				<video :src="video_url_text" object-fit="cover" class="upzhizhao"></video>
			</view>
		</view>
		<view class="ffffff mainpadding_top flexbetween " @click="selectyg()">
			<view class="hei_text bold titletext">可用員工</view>
			<view class="flexright">
				<input type="text" class="hei_text main_size bold margin_right1" disabled="" style="text-align: right;"
					placeholder="請選擇可用員工" :value="shopstaff.length?'已選擇':''">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		<view class="ffffff mainpadding_top flexbetween " @click="selectrole()">
			<view class="hei_text bold titletext">可用分店</view>
			<view class="flexright">
				<input type="text" class="hei_text main_size bold margin_right1" disabled="" style="text-align: right;"
					placeholder="請選擇可用分店" :value="rallshop.length?'已選擇':''">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		<view class="mainpadding_top ffffff flexbetween" @click="addspu()">
			<view class="flexleft">
				<view class="titletext bold hei_text">產品規格2</view>
				<view class="" @click="spu_data=[]">
					<u-icon name="trash" color="#ff0000" size="20"></u-icon>
				</view>
			</view>
			<view class="flexright">
				<input class="main_size bold hei_text textright" type="text" placeholder="請選擇產品規格2" disabled
					v-if="!spu_data.length">
				<view class="main_size bold hei_text textright" v-else>已選擇</view>
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		<view class="mainpadding huang_text titletext bold">調查時間</view>
		<view class="ffffff mainpadding flexbetween " @click="messageTime.timeShow=true;timevalue=0">
			<view class="hei_text bold titletext">開始</view>
			<view class="flexright">
				<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;" disabled=""
					v-model="dc_starttime" placeholder="請選擇開始時間">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		<view class="ffffff mainpadding_top flexbetween " @click="messageTime.timeShow=true;timevalue=1">
			<view class="hei_text bold titletext">結束</view>
			<view class="flexright">
				<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;" disabled=""
					v-model="dc_endtime" placeholder="請選擇結束時間">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		<template v-if="listid">
			<view class="mainpadding huang_text titletext bold">領取時間</view>
			<view class="ffffff mainpadding flexbetween " @click="messageTime.timeShow=true;timevalue=2">
				<view class="flexleft">
					<view class="hei_text bold titletext">開始</view>
					<view class="" @click.stop="get_starttime=''">
						<u-icon name="trash" color="#ff0000" size="16"></u-icon>
					</view>
				</view>
				<view class="flexright">
					<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;" disabled=""
						v-model="get_starttime" placeholder="請選擇開始時間">
					<u-icon name="arrow-right" color="#999" size="16"></u-icon>
				</view>
			</view>
			<view class="ffffff mainpadding_top flexbetween " @click="messageTime.timeShow=true;timevalue=3">
				<view class="flexleft">
					<view class="hei_text bold titletext">結束</view>
					<view class="" @click.stop="get_endtime=''">
						<u-icon name="trash" color="#ff0000" size="16"></u-icon>
					</view>
				</view>
				<view class="flexright">
					<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;" disabled=""
						v-model="get_endtime" placeholder="請選擇結束時間">
					<u-icon name="arrow-right" color="#999" size="16"></u-icon>
				</view>
			</view>
		</template>
		<timePicker :messageTime="messageTime" @timeConfirm="timeConfirm"></timePicker>
		<view class="mainpadding">
			<view class="submitbtn" @click="submit">確認</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: "",
				share_title: "",
				rallshop: [],
				get_starttime: "",
				get_endtime: "",
				messageTime: {
					timeShow: false,
					title: "選擇時間",
					mode: "datetime", //date日期，datetime年月日时分秒，time時間，year-month年月
				},
				timevalue: 0, //0使用開始，1使用结束，2領取開始，3領取結束
				listid: "",
				type: "1",
				dc_starttime: "",
				dc_endtime: "",
				get_type: false,
				image_text: [],
				image: [],
				issubmit: false,
				spu_data: [],
				video_url_text: "",
				video_url: "",
				images_text: [],
				images: [],
				shopstaff:[]
			}
		},
		onLoad(options) {
			if (options.id) {
				this.listid = options.id
				this.getDetail()
			}
			uni.$on("checkfd", data => {
				this.rallshop = data
			})
			uni.$on("checkyg", data => {
				this.shopstaff = data
			})
			uni.$on("checkfxdcFn", data => {
				this.spu_data = data
				console.log(data);
			})
			
		},
		onUnload() {
			uni.$off("checkfd")
			uni.$off("checkyg")
			uni.$off("checkfxdcFn")
		},
		methods: {
			upvideo() { //上传影片
				let uploadFile = ''
				let _this = this;
				// 1.选择要上传的影片
				uni.chooseVideo({
					maxDuration: 60, // 拍摄影片最长拍摄时间，單位秒。最长支持 60 秒。
					sourceType: ['album', 'camera'], // album 从相册选影片，camera 使用相機拍摄，默认为：['album', 'camera']
					success: function(res) {
						_this.uploadImageServicet(res.tempFilePath)
					}
				});
			},
			uploadImageServicet(url, upImg, upImgs_text) {
				let _this = this;
				this.http.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					this.video_url = res.url
					this.video_url_text = image_item
				}).catch(err => {});
			},
			delvideo() {
				this.video_url = ""
				this.video_url_text = ""
			},
			addspu() {
				uni.navigateTo({
					url: "/pages/workbench/shangpingge?type=1&msg=" + encodeURIComponent(JSON.stringify(this
						.spu_data))
				})
			},
			removeImg(arr, arr_text, index) {
				arr.splice(index, 1)
				arr_text.splice(index, 1)
			},
			getDetail() {
				uni.showLoading({
					mask: true,
					title: "請稍後"
				})
				this.http.request("/api/Shopwelfarecl/shopwelfareDetail", 'POST', {
					id: this.listid,
					shop_id:uni.getStorageSync("shop_id")
				}).then(res => {
					uni.hideLoading()
					let data = res.data
					this.name = data.name
					this.share_title = data.share_title
					let arr = []
					data.shop_data.forEach(item => {
						arr.push({
							shop_id: item.id,
						})
					})
					this.rallshop = arr
					let arr2 = []
					data.spu_data_text.forEach(item => {
						let goods_spunat = []
						item.spu_item.forEach(cur => {
							goods_spunat.push({
								image_arr: [cur.image_text],
								images: [cur.image],
								money: cur.money,
								name: cur.name,
								yg_money:cur.yg_money
							})
						})
						arr2.push({
							arrtName: "",
							attrId: [],
							fh: item.fh,
							id: "",
							name: item.name,
							num_b: item.num_b,
							goods_spunat,
							ygfh: item.ygfh,
							font_color:item.font_color,
							ygnum_b: item.ygnum_b
						})
					})
					this.spu_data = arr2
					this.image = data.image ? [data.image] : []
					this.image_text = data.image_text ? [data.image_text] : []
					if(data.get_starttime){
						this.get_starttime = this.http.timereturn(data.get_starttime * 1000)
					}
					if(data.get_endtime){
						this.get_endtime = this.http.timereturn(data.get_endtime * 1000)
					}
					this.dc_starttime = this.http.timereturn(data.dc_starttime * 1000)
					this.dc_endtime = this.http.timereturn(data.dc_endtime * 1000)
					this.images = data.images.split(',')
					this.images_text = data.images_text
					this.video_url = data.video_url
					this.video_url_text = data.video_url_text
					this.shopstaff = data.shopstaff_id.split(',')
				})
			},
			selectrole() {
				let data = {
					type: 1,
					rallshop: this.rallshop,
					get_type: this.get_type
				}
				uni.navigateTo({
					url: "/pages/workbench/fendianlb?msg=" + encodeURIComponent(JSON.stringify(data))
				})
			},
			selectyg(){
				let data = {
					type: 1,
					shopstaff: this.shopstaff,
				}
				uni.navigateTo({
					url: "/pages/workbench/yuangonggl?msg=" + encodeURIComponent(JSON.stringify(data))
				})
			},
			async submit() {
				if (this.issubmit) return
				if (this.name == "") {
					this.http.toast("請輸入活動名稱")
					return false
				}
				if (this.image.length == 0) {
					this.http.toast("請上傳圖片")
					return false
				}
				if (this.images.length == 0) {
					this.http.toast("輪播圖至少上傳一張")
					return false
				}
				// if (!this.rallshop.length) {
				// 	this.http.toast("請選擇可用分店")
				// 	return false
				// }
				// if (this.get_starttime == "") {
				// 	this.http.toast("請選擇領取開始時間")
				// 	return false
				// }
				// if (this.get_endtime == "") {
				// 	this.http.toast("請選擇領取結束時間")
				// 	return false
				// }
				if (this.dc_starttime == "") {
					this.http.toast("請選擇調查開始時間")
					return false
				}
				if (this.dc_endtime == "") {
					this.http.toast("請選擇調查結束時間")
					return false
				}
				let fenshop_arr = []
				this.rallshop.forEach(item => {
					if (item.id == "") delete item.id
					fenshop_arr.push(item.shop_id)
				})
				let data = []
				this.spu_data.forEach(item => {
					let spu_item = []
					item.goods_spunat.forEach(cur => {
						spu_item.push({
							name: cur.name,
							money: cur.money,
							image: cur.images[0] || "",
							yg_money:cur.yg_money
						})
					})
					data.push({
						name: item.name,
						num_b: item.num_b,
						fh: item.fh,
						spu_item,
						ygfh: item.ygfh,
						font_color:item.font_color,
						ygnum_b: item.ygnum_b
					})
				})
				let datamsg = {
					shop_id: uni.getStorageSync("shop_id"),
					name: this.name,
					share_title: this.share_title,
					get_starttime: this.get_starttime,
					get_endtime: this.get_endtime,
					dc_starttime: this.dc_starttime,
					dc_endtime: this.dc_endtime,
					image: this.image[0],
					images:this.images.join(),
					video_url:this.video_url,
					shopstaff_id:this.shopstaff.join(),
					fenshop_id: fenshop_arr.join(),
					spu_data: data
				}
				let url = "/api/Shopwelfarecl/addShopWelfare"
				// if (this.listid) {
				// 	url = "/api/Shopwelfarecl/editShopWelfare"
				// 	this.$set(datamsg, "id", this.listid)
				// }
				this.issubmit = true
				uni.showLoading({
					mask: true,
					title: "請稍後"
				})
				let res = await this.http.request(url, 'POST', datamsg)
				uni.hideLoading()
				if (res.code == 1) {
					this.http.toast(res.msg)
					setTimeout(() => {
						uni.navigateBack(1)
					}, 500)
				} else {
					this.issubmit = false
					this.http.toast(res.msg)
				}
			},
			timeConfirm(e) {
				if (this.timevalue == 0) {
					this.dc_starttime = e
				}
				if (this.timevalue == 1) {
					this.dc_endtime = e
				}
				if (this.timevalue == 2) {
					this.get_starttime = e
				}
				if (this.timevalue == 3) {
					this.get_endtime = e
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep.u-radio-group {
		flex: none !important;
	}
	.upzhizhao {
		width: 100%;
		height: 300rpx;
		border-radius: 10rpx;
		box-sizing: border-box;
		background-color: #f7f7f7;
	}
</style>